#include <stdio.h>
#include <string.h>
#include "mpi.h"

int main(int argc, char *argv[])
{
	int rank, size, root = 0, msg_size = 20;
	char message[msg_size + 1];
    

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

	/* The first process get the message from the user. */
    if (rank == 0) {
    	fprintf(stderr, "Please input a message(maximum 20 characters): ");
    	fgets(message, msg_size + 1, stdin);
    	if(strlen(message) < msg_size)
    		message[strlen(message) - 1] = '\0';
    }

	/* All the processes do the broadcast and display the received message. */
    MPI_Bcast(message, msg_size + 1, MPI_CHAR, root, MPI_COMM_WORLD);
    printf("Process %d receiving a message: %s\n", rank, message);
    
    MPI_Finalize();
    return 0;
}
